package io.gitee.yanbinchen;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author: alex
 * @since : 2025-02-28 18:02
 */
public class SuperCacheBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof SuperCacheService) {
            CacheContextHolder.setSuperCacheService((SuperCacheService) bean);
        }
//        Method[] methods = bean.getClass().getDeclaredMethods();
//        boolean needProxy = false;
//        for (Method method : methods) {
//            if (method.isAnnotationPresent(SuperKey.class)) {
//                SuperKey superKey = method.getAnnotation(SuperKey.class);
//                MetaInfo metaInfo = new MetaInfo(bean.getClass().getName(), method.getName(), method.getParameters(), superKey, null);
//                CacheContextHolder.put(metaInfo.getMetaId(), metaInfo.getSuperKey());
//                needProxy = true;
//            }
//            if (method.isAnnotationPresent(DelSuperKey.class)) {
//                DelSuperKey delSuperKey = method.getAnnotation(DelSuperKey.class);
//                MetaInfo metaInfo = new MetaInfo(bean.getClass().getName(), method.getName(), method.getParameters(), null, delSuperKey);
//                CacheContextHolder.putDel(metaInfo.getMetaId(), metaInfo.getDelSuperKey());
//                needProxy = true;
//            }
//        }
//        if (needProxy) {
//            ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
//            proxyFactoryBean.setTarget(bean);
//            proxyFactoryBean.addAdvice(new SuperCacheInterceptor());
//            return proxyFactoryBean.getObject();
//        }
        return bean;
    }
}
